﻿using AutoMapper;
using EFEntity;
using ISerives;
using Model;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Serives
{
    public class usersServices : IusersServices
    {

        private readonly MyDbContext myDbContext;
        private readonly IMapper mapper;
        public usersServices(MyDbContext myDbContext, IMapper mapper)
        {
            this.myDbContext = myDbContext;
            this.mapper = mapper;
        }

        public async Task<int> Add(users s)
        {
            myDbContext.sers.Add(s);
            int num = await myDbContext.SaveChangesAsync();
            return num;
        }

        public async Task<int> Delete(int id)
        {
            users ef = new users()
            {
                user_id = id
            };
            myDbContext.sers.Attach(ef);
            myDbContext.sers.Remove(ef);
            int num = await myDbContext.SaveChangesAsync();
            return num;
        }

        public async Task<List<Musers>> Select()
        {
            List<Musers> list1 = new List<Musers>();
            await Task.Run(() =>
            {
                List<users> list = myDbContext.sers.ToList();
                list1 = mapper.Map<List<Musers>>(list);

            });
            return list1;
        }

        public async Task<users> Up(int id)
        {
            users a = new users();
            await Task.Run(() =>
            {
                a = myDbContext.sers.Where(e => e.user_id == id).FirstOrDefault();

            });
            return a;
        }

        public async Task<int> Update(users s)
        {
            users es = myDbContext.sers.Where(es => es.user_id == s.user_id).FirstOrDefault();
            es.user_name = s.user_name;
            es.user_true_name = s.user_true_name;
            es.user_password = s.user_password;
            es.user_sf = s.user_sf;
            return await myDbContext.SaveChangesAsync();
        }
    }
}
